import Taro from '@tarojs/taro'
export * from './request'

export const generateUUID = () => {
  let d = new Date().getTime()
  const uuids = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    // eslint-disable-next-line no-bitwise
    const r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    // eslint-disable-next-line eqeqeq, no-bitwise
    return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
  })
  return uuids
}

export const tryHttpError = (http, code = (e) => e >= 200 && e < 400) => {
  return new Promise((resolve, reject) => {
    http.then((res) => {
      console.log('tryHttpError', res)
      if (code(res.statusCode) && res?.data?.code === 0) {
        resolve(res?.data)
      } else {
        Taro.showToast({ title: res?.data?.message || '系统异常，请稍后重试', icon: 'none' })
        reject(res)
      }
    }).catch((err) => {
      console.log('err', err)
      Taro.showToast({ title: err?.data?.message || '系统异常，请稍后重试', icon: 'none' })
      reject(err)
    })
  })
}
